﻿#region License Information (GPL v2)
/*
    Source Queries - Source Server Queries
    Copyright (C) 2009  Source Queries Developers

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
    Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SourceQueries
{
    public static class Extensions
    {
        public static string ToStringUntilNull(this byte[] bytes, int index)
        {
            for (int i = index; i < bytes.Length; i++)
            {
                if (bytes[i] == 0x00)
                {
                    return Encoding.ASCII.GetString(bytes, index, i - index);
                }
            }
            return "";
        }

        public static string ToString(this byte[] bytes)
        {
            return Encoding.UTF8.GetString(bytes);
        }

        public static string ToString(this byte[] bytes, int index, int count)
        {
            return Encoding.UTF8.GetString(bytes, index, count);
        }

        public static string ToHex(this byte[] bytes)
        {
            return string.Join(" ", bytes.Select(x => x.ToString("X2")).ToArray());
        }

        public static int ToInt32(this byte[] bytes)
        {
            return BitConverter.ToInt32(bytes, 0);
        }

        public static int ToInt32(this byte[] bytes, int index)
        {
            return BitConverter.ToInt32(bytes, index);
        }
    }
}